<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试JSON数据</title>
</head>
<body>
    <form id="user">
        <div>用户名： <input type="text" name="userName" id="userName"></div>
        <div>密码： <input type="password" name="password" id="password"></div>
        <div>年龄： <input type="text" name="age" id="age"></div>
        <div>邮箱： <input type="text" name="email" id="email"></div>
        <div>手机号： <input type="text" name="phoneNo" id="phoneNo"></div>
    </form>

    <button id="test01">test01</button>
    <button id="test02">test02</button>
    <button id="test03">test03</button>
    <button id="test04">test04</button>
    <button id="test05">test05</button>
</body>
</html>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>

    $.fn.serializeObject = function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return JSON.stringify(o); <!--将json对象转成json字符串-->
    }



    $(function(){
        $("#test05").click(function (){
            var data =$("#user").serializeObject();
            console.log(data);

            //发起ajax请求
            $.ajax({
                method:"post",
                url:"test05",
                data:data,
                dataType:"json",
                contentType:"application/json",
                success:function (result){
                    console.log(result);
                }
            });
        });


        $("#test01").click(function (){
            var data = {"userName":$("#userName").val(),"password":$("#password").val()};
            //发起ajax请求
            $.ajax({
                method:"post",
                url:"test01",
                data:data,
                type:"json",
                success:function (result){
                    console.log(result);
                }
            });
        });

        $("#test02").click(function (){
            var data = {"userName":$("#userName").val(),"password":$("#password").val(),"age":$("#age").val()};
            //发起ajax请求
            $.ajax({
                method:"post",
                url:"test02",
                data:data,
                type:"json",
                success:function (result){
                    console.log(result);
                }
            });
        });

        $("#test03").click(function (){
            var data = {"userName":$("#userName").val(),"password":$("#password").val()};
            //发起ajax请求
            $.ajax({
                method:"post",
                url:"test03",
                data:data,
                type:"json",
                success:function (result){
                    console.log(result);
                }
            });
        });
        $("#test04").click(function (){
            var data = {"userName":$("#userName").val(),"password":$("#password").val(),"age":$("#age").val()};
            //发起ajax请求
            $.ajax({
                method:"post",
                url:"test04",
                data:data,
                type:"json",
                success:function (result){
                    console.log(result);
                }
            });
        });

    });
</script>